<?php

namespace App\Http\Controllers\Admin;

use App\Models\Admin;
use App\Models\Artcat;
use App\Models\ArticleClass;
use App\Models\Service;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ServiceController extends Controller
{

    public function index(Request $request)
    {
        $limit = $request->get('limit', 10);
        $house_id = $request->get('house_id', 0);
        $mobile = $request->get('mobile');
        $keys = $request->get('keys');
        $status = $request->get('status', 0);
        $model = Service::query();
        if ($keys) {
            $model = $model->where('title', 'like', '%' . $keys . '%');
        }
        if ($mobile) {
            $model = $model->where('mobile', $mobile);
        }
        if ($house_id) {
            $model = $model->where('house_id', $house_id);
        }
        if ($status == 1) {
            $model = $model->where('flag', $status);
        } else {
            if ($status == 2) {
                $model = $model->where('flag', 0);
            }
        }
        $result = $model->orderBy('orders', 'asc')->orderBy('id','desc')->paginate($limit);
        foreach ($result as $value) {
            if ($value['headimg']) {
                $value['headimg'] = getAliOssImage($value['headimg']);
            }
        }
        return showMsg('操作成功', 200, $result);
    }


    public function detail(Request $request)
    {
        $id = $request->get('id');
        $result = Service::findOrFail($id);
        if (!$result) {
            return showMsg('信息已删除', 201, $result);
        }
        if ($result['headimg']) {
            $result['headimg'] = getAliOssImage($result['headimg']);
        }
        return showMsg('操作成功', 200, $result);
    }


    public function beforeSave(Request $request)
    {
        $id = $request->get('id');
        $data = $request->all();
        if($data['headimg']){
            $data['headimg'] = delAliOssImage($data['headimg']);
        }
        if ($id) {
            $article = Service::findOrFail($id);
            if ($article->update($data)) {
                return showMsg('更新成功', 200);
            }
        } else {
            Service::create($data);
            return showMsg('添加成功', 200);
        }
    }


    public function destroy(Request $request)
    {
        $ids = $request->get('ids');
        if (empty($ids)) {
            return showMsg('请选择操作项', 201);
        }
        if (is_array($ids)) {
            foreach ($ids as $v) {
                Service::destroy('id', $v);
            }
        } else {
            Service::destroy('id', $ids);
        }
        return showMsg('删除成功', 200);
    }

    public function setStatus(Request $request)
    {
        $id = $request->get('id');
        $status = $request->get('status');
        $re = Service::where('id', $id)->first();
        if ($re['flag'] == 1) {
            Service::where('id', $id)->update(['flag' => 0]);
        } else {
            Service::where('id', $id)->update(['flag' => 1]);
        }
        return showMsg('设置成功', 200);
    }

}
